package com.dl.code.realm;

import com.dl.code.entity.Role;
import com.dl.code.entity.RolePermission;
import com.dl.code.entity.User;
import com.dl.code.entity.UserRole;
import com.dl.code.service.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * 作者: DL代先生
 * 日期: 2021/5/28
 * 时间: 20:08
 * 内容: Shiro与springboot整合教程！
 * 描述: 自定义Realm
 */
public class UserRealm extends AuthorizingRealm {
    @Autowired
    private UserService userService;

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private RolePermissionService rolePermissionService;

    @Autowired
    private PermissionService permissionService;

    @Override
    public String getName() {
        return "UserRealm";
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        Session session = SecurityUtils.getSubject().getSession(false);
        User user = (User) session.getAttribute("user");
        UserRole userRole = userRoleService.selectAllUserRoles(user.getUserId());
        Role role = roleService.selectRoleById(userRole.getRoleId());
        List<RolePermission> rolePermissions = rolePermissionService.selectAllRolePermissions(role.getRoleId());
        List<Integer> idLists = new ArrayList<>();
        for (RolePermission rolePermission : rolePermissions) {
            idLists.add(rolePermission.getPermissionId());
        }
        List<String> permissions = permissionService.selectAllPermissions(idLists);
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.addRole(role.getRoleName());
        simpleAuthorizationInfo.setStringPermissions(new HashSet<>(permissions));
        return simpleAuthorizationInfo;
    }

    /**
     * 获得认证的时候需要的安全数据，数据是从数据库中获取的
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String principal = (String) authenticationToken.getPrincipal();
        User user = userService.selectUserByAccount(principal);
        if (user != null){
            SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(
                    principal,
                    user.getUserPassword(),
                    ByteSource.Util.bytes(user.getUserSalt()),
                    getName());
            Session session = SecurityUtils.getSubject().getSession();
            session.setAttribute("user",user);
            return simpleAuthenticationInfo;
        }
        return null;
    }
}
